﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Kangaroo.Controls.Tests
{
    class TreeNodeSerializer
    {
        public string Serialize(params TreeNode[] treeNodes)
        {
            var stringWriter = new StringWriter();
            var xmlTextWriter = new XmlTextWriter(stringWriter);
            foreach(var treeNode in treeNodes)
            {
                DoSerialize(treeNode, xmlTextWriter);
            }
            return stringWriter.ToString();
        }

        private static void DoSerialize(TreeNode treeNode, XmlWriter writer)
        {
            writer.WriteStartElement("n");
            writer.WriteAttributeString("t", treeNode.Text);
            foreach (TreeNode childNode in treeNode.Nodes)
            {
                DoSerialize(childNode, writer);
            }
            writer.WriteEndElement();
        }
    }
}
